#include <string>
#include <stdexcept>

class Computer {
private:
    std::string cpu;
    std::string memory;
    std::string hardDisk;

    // 私有构造函数，仅允许Builder访问
    Computer() = default;
    friend class Builder;  // 声明Builder为友元类

public:
    // Getter方法
    std::string getCPU() const { return cpu; }
    std::string getMemory() const { return memory; }
    std::string getHardDisk() const { return hardDisk; }
};
  // 建造者类
  class Builder {
    private:
        Computer computer;  // 内部维护的Computer实例

    public:
        Builder& setCPU(const std::string& cpu) {
            computer.cpu = cpu;
            return *this;  // 返回引用以支持链式调用
        }

        Builder& setMemory(const std::string& memory) {
            computer.memory = memory;
            return *this;
        }

        Builder& setHardDisk(const std::string& hardDisk) {
            computer.hardDisk = hardDisk;
            return *this;
        }

        Computer build() {
            if (computer.cpu.empty()) {
                throw std::runtime_error("CPU未配置");
            }
            return computer;  // 返回构建完成的实例
        }
    };
class Director {
    public:
        static Computer buildStandardComputer(Builder builder) {
            return builder
                .setCPU("Intel i5")
                .setMemory("16GB DDR4")
                .setHardDisk("512GB SSD")
                .build();
        }
    };
    
int main() {
    try {
        Computer highEndPC = Builder()
            .setCPU("Intel i9")
            .setMemory("32GB DDR5")
            .setHardDisk("1TB SSD")
            .build();
        
        // 使用highEndPC...
    } catch (const std::exception& e) {
        // 处理异常
    }
     // 使用示例
    Computer standardPC = Director::buildStandardComputer(Builder());
    return 0;
}
